// pages/index/yl_style/yl_style.js
import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog"

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isRefreshStyle: false,
    stylePageNumber: 1,
    styleList: [],
    tabIndex: 0,
    tabList: [{
      title: "进行中",
      status: 1,
      pageNumber: 1,
      isRefresh: false,
      list: []
    },{
      title: "待开展",
      status: 2,
      pageNumber: 1,
      isRefresh: false,
      list: []
    },{
      title: "已结束",
      status: 0,
      pageNumber: 1,
      isRefresh: false,
      list: []
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getActivityList()
    this.getStyleList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * tab 页切换
   * @param e
   */
  onTabChange: function (e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      tabIndex: index
    })
    if (this.data.tabList[index].list.length <= 0) {
      this.getActivityList()
    }
  },

  /**
   * 社区活动下拉刷新
   * @param e
   */
  onRefreshActivity: function (e) {
    this.data.tabList[this.data.tabIndex].list = []
    this.data.tabList[this.data.tabIndex].isRefresh = true
    this.data.tabList[this.data.tabIndex].pageNumber = 1
    this.setData({
      tabList: this.data.tabList
    })
    this.getActivityList()
  },

  /**
   * 社区活动上拉加载更多
   * @param e
   */
  onLowerActivity: function (e) {
    this.data.tabList[this.data.tabIndex].pageNumber++
    this.getActivityList()
  },

  /**
   * 社区活动详情
   * @param e
   */
  onActivityDetail: function (e) {
    const id = e.currentTarget.dataset.id
    const tabIndex = this.data.tabIndex
    const status = this.data.tabList[tabIndex].status
    let type = status == 1 ? 0 : status == 2 ? 1 : 2
    wx.navigateTo({
      url: "../activity/activityindex/activityindex?id=" + id + "&type=" + type
    })
  },

  /**
   * 获取活动列表
   */
  getActivityList: function () {
    Toast.loading({
      duration: 0,
      message: "加载中..."
    })
    const tabIndex = this.data.tabIndex
    http.get("activity/list",{
      area_code: app.globalData.areaCode,
      status: this.data.tabList[tabIndex].status,
      page_number: this.data.tabList[tabIndex].pageNumber,
      page_size: 10,
    }).then(res => {
      Toast.clear()
      if (res.data.code === http.REQUEST_OK) {
        if (res.data.data.list.length > 0) {
          res.data.data.list.forEach(item => {
            this.data.tabList[tabIndex].list.push(item)
          })
        } else {
          if (this.data.tabList[tabIndex].pageNumber > 1) {
            this.data.tabList[tabIndex].pageNumber--
          }
          Toast.fail("没有更多数据了")
        }
      } else {
        if (this.data.tabList[tabIndex].pageNumber > 1) {
          this.data.tabList[tabIndex].pageNumber--
        }
        Toast.fail(res.data.msg)
      }
      this.data.tabList[tabIndex].isRefresh = false
      this.setData({
        tabList: this.data.tabList
      })
    }, err => {
      Toast.clear()
      Toast.fail("网络异常，请稍后再试！")
      this.data.tabList[tabIndex].isRefresh = false
      this.setData({
        tabList: this.data.tabList
      })
    })

  },

  /**
   * 社区风采下拉刷新
   * @param e
   */
  onRefreshStyle: function (e) {
    this.setData({
      stylePageNumber: 1,
      styleList: [],
      isRefreshStyle: true
    })
    this.getStyleList()
  },

  /**
   * 社区风采上拉加载更多
   * @param e
   */
  onLowerStyle: function (e) {
    this.data.stylePageNumber++
    Toast.loading({
      duration: 0,
      message: "加载中..."
    })
    this.getStyleList()
  },

  /**
   * 社区风采详情
   * @param e
   */
  onStyleDetail: function (e) {
    const id = e.currentTarget.dataset.id
    const content_id = e.currentTarget.dataset.content_id
    wx.navigateTo({
      url: "../community_style/detail/detail?id=" + id + "&content_id=" + content_id
    })
  },


  /**
   * 获取社区风采列表
   */
  getStyleList: function () {
    this.setData({
      isLoading: true
    })
    http.get("notice/open/page",{
      type: 4,
      applet_id:app.globalData.applet_id,
      page_number: this.data.stylePageNumber,
      page_size: 10,
      unit_code: app.globalData.unitCode,
    }).then(res => {
      Toast.clear()
      if (res.data.code === http.REQUEST_OK) {
        if (res.data.data.list.length > 0) {
          res.data.data.list.forEach(item => {
            this.data.styleList.push(item)
          })
        } else {
          if (this.data.stylePageNumber > 1) {
            this.data.stylePageNumber--
          }
          Toast.fail("没有更多数据了")
        }
        this.setData({
          styleList: this.data.styleList,
          isRefreshStyle: false,
          isLoading: false
        })
      }else {
        if (this.data.stylePageNumber > 1) {
          this.data.stylePageNumber--
        }
        Dialog.alert({
          title: "失败",
          message: "获取失败\n" + res.data.msg
        }).then(() => {

        })
        this.setData({
          isRefreshStyle: false,
          isLoading: false
        })
      }
    }, err => {
      Toast.clear()
      Toast.fail("网络异常")
      if (this.data.stylePageNumber > 1) {
        this.data.stylePageNumber--
      }
      this.setData({
        isRefreshStyle: false,
        isLoading: false
      })
    })
  },


})